﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;
using System.Windows.Browser;

namespace Baosight.ZNH.ZMCL.UI
{
    /// <summary>
    /// 安全隐患日报列表页面
    /// </summary>
    /// Title:ZMCLR0
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2011-05-30  高强   [创建]
    ///  
    public partial class ZMCLR0 : EFViewCmes
    {
        private AddNewReportBlock block = new AddNewReportBlock();//构造前台下拉列表
        private string date;

        public ZMCLR0()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 新增/修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.Grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            initData.AddBlock(block.getReportType());
            base.Initialize(initData);
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "安全日报-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.Grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.Grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
                string fdStatus = row["fdStatus"] == null ? "" : row["fdStatus"].ToString();

                if ("生效".Equals(fdStatus))
                {
                    this.OpenEFWindowAlert("该纪录已经生效，不能修改！", true);
                    return;
                }
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "安全日报-修改", "update", null, sender, e);
        }

        /// 预览安全隐患日报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PerviewButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hyperlink = sender as HyperlinkButton;
            base.TempHyperlink = hyperlink;

            EIRow row = (hyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            Dictionary<string, object> args = new Dictionary<string, object>();
            args.Add("fdReportId", row["fdKeyId"]);//传递隐患日报主键
            date = row["fdReportDate"].ToString();

            HttpServiceAgent agent = new HttpServiceAgent();
            EIInfo eiInfo = new EIInfo();
            eiInfo.SetAttributes(args);
            eiInfo.Attributes.Add(EIConstants.ServiceNameKey, "ZMCLR2");
            eiInfo.Attributes.Add(EIConstants.MethodNameKey, "queryAll");
            agent.CallService(eiInfo, OnPerviewQueryWorkCompleted);
        }

        /// <summary>
        /// 预览安全隐患日报的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnPerviewQueryWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                if (e.EIInfo.GetBlock("result").GetRowCount() == 0 && e.EIInfo.Get("count").Equals("0"))
                {
                    base.OpenEFWindowAlert("当前报表无数据，请确认！", true);
                    //超链接生效
                    if (base.TempHyperlink != null)
                    {
                        base.TempHyperlink.IsEnabled = true;
                        base.TempHyperlink = null;
                    }
                    return;
                }
                else
                {
                    Uri uri = Uploader.ConstructAbsoluteUri("../admin/RunmaxReport.jsp?projectName=CMESReport&reportName=daliy&ReportDate=" + date + "&IsShow=0");
                    HtmlPage.Window.Navigate(uri, "_blank");
                }

                //按钮生效
                base.TempButtonOpen(true);
            }
            else
            {
                EFWindow.Alert(e.EIInfo.GetMsg());
            }
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, WindowClosedEventArgs e)
        {
            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 生效按钮的执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPublishButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("publish", "生效", sender, e);
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 构造弹出新增日报页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        /// <param name="dialogType"></param>
        /// <param name="arg"></param>
        public override void OpenPopup(EIInfo eIInfo, string title, string dialogType, string arg)
        {
            AddNewDailyReport addNewDailyReport = new AddNewDailyReport(dialogType);//构造一个新的修改页面
            addNewDailyReport.EIDataContext = new Platform.Core.UI.EIDataContext();
            eIInfo.AddBlock(block.getReportType());//过滤类型
            eIInfo.AddBlock(block.getReportStauts());
            addNewDailyReport.EIDataContext.Update(eIInfo);

            addNewDailyReport.fdStatus.Value = "草拟";
            addNewDailyReport.fdType.Value = "0";
            addNewDailyReport.fdTitle.Value = "安全隐患日报";

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = title == null ? "" : title;//设置Title
            _popup.Content = addNewDailyReport;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(ParentInsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 主表弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ParentInsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.Grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 页签切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EFTab_SelectionChanged(object sender, RoutedEventArgs e)
        {
            EFTab _tab = sender as EFTab;
            if (null != _tab)
            {
                EFTabItem _item = _tab.SelectedItem as EFTabItem;

                if (null != _item && _item.Name == "riskRecord")
                {
                    if (Grid.SelectedItem != null)
                    {
                        var fdDate = this.Grid.SelectedItem.GetType().GetProperty("fdReportDate").GetValue(this.Grid.SelectedItem, null);
                        var fdKeyId = this.Grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.Grid.SelectedItem, null);
                        var fdStatus = this.Grid.SelectedItem.GetType().GetProperty("fdStatus").GetValue(this.Grid.SelectedItem, null);

                        Dictionary<string, string> _args = new Dictionary<string, string>();
                        _args.Add("fdRiskDate", fdDate.ToString());
                        _args.Add("fdStatus", fdStatus.ToString());
                        _args.Add("fdReportId", fdKeyId.ToString());
                        _args.Add("fdType", "安全隐患");

                        this.riskRecord.LoadView<EFTabItem>("ZMCLR2", _args);//ZMCLR2 安全隐患
                    }
                    else
                    {
                        this.riskRecord.Content = null;
                    }
                }

                if (null != _item && _item.Name == "riskCP")
                {
                    if (Grid.SelectedItem != null)
                    {
                        var fdDate = this.Grid.SelectedItem.GetType().GetProperty("fdReportDate").GetValue(this.Grid.SelectedItem, null);
                        var fdKeyId = this.Grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.Grid.SelectedItem, null);
                        var fdStatus = this.Grid.SelectedItem.GetType().GetProperty("fdStatus").GetValue(this.Grid.SelectedItem, null);

                        Dictionary<string, string> _args = new Dictionary<string, string>();
                        _args.Add("fdRiskDate", fdDate.ToString());
                        _args.Add("fdStatus", fdStatus.ToString());
                        _args.Add("fdReportId", fdKeyId.ToString());
                        _args.Add("fdType", "工程质量及文明生产");

                        this.riskCP.LoadView<EFTabItem>("ZMCLR2", _args);//ZMCLR2 工程质量及文明生产
                    }
                    else
                    {
                        this.riskCP.Content = null;
                    }
                }

                if (null != _item && _item.Name == "riskPG")
                {
                    if (Grid.SelectedItem != null)
                    {
                        var fdDate = this.Grid.SelectedItem.GetType().GetProperty("fdReportDate").GetValue(this.Grid.SelectedItem, null);
                        var fdKeyId = this.Grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.Grid.SelectedItem, null);
                        var fdStatus = this.Grid.SelectedItem.GetType().GetProperty("fdStatus").GetValue(this.Grid.SelectedItem, null);

                        Dictionary<string, string> _args = new Dictionary<string, string>();
                        _args.Add("fdDate", fdDate.ToString());
                        _args.Add("fdStatus", fdStatus.ToString());
                        _args.Add("fdReportId", fdKeyId.ToString());

                        this.riskPG.LoadView<EFTabItem>("ZMCLR7", _args);//ZMCLR7 工程质量和文明生产日评估
                    }
                    else
                    {
                        this.riskPG.Content = null;
                    }
                }

                if (null != _item && _item.Name == "civilizationProduce")
                {
                    if (Grid.SelectedItem != null)
                    {
                        var fdDate = this.Grid.SelectedItem.GetType().GetProperty("fdReportDate").GetValue(this.Grid.SelectedItem, null);
                        var fdKeyId = this.Grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.Grid.SelectedItem, null);
                        var fdStatus = this.Grid.SelectedItem.GetType().GetProperty("fdStatus").GetValue(this.Grid.SelectedItem, null);

                        Dictionary<string, string> _args = new Dictionary<string, string>();
                        _args.Add("fdDate", fdDate.ToString());
                        _args.Add("fdStatus", fdStatus.ToString());
                        _args.Add("fdReportId", fdKeyId.ToString());

                        this.civilizationProduce.LoadView<EFTabItem>("ZMCLR6", _args);//ZMCLR6 文明生产
                    }
                    else
                    {
                        this.civilizationProduce.Content = null;
                    }
                }
            }
        }

        /// <summary>
        /// 选中主表一条记录，显示该条记录的从表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            EFTabItem _item = this.eFTab.SelectedItem as EFTabItem;

            if (Grid.SelectedItem != null)
            {
                if (null == _item)
                {
                    _item.Content = null;
                }
                else if (_item.Name == "riskRecord")
                {
                    var fdDate = this.Grid.SelectedItem.GetType().GetProperty("fdReportDate").GetValue(this.Grid.SelectedItem, null);
                    var fdKeyId = this.Grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.Grid.SelectedItem, null);
                    var fdStatus = this.Grid.SelectedItem.GetType().GetProperty("fdStatus").GetValue(this.Grid.SelectedItem, null);

                    Dictionary<string, string> _args = new Dictionary<string, string>();
                    _args.Add("fdRiskDate", fdDate.ToString());
                    _args.Add("fdStatus", fdStatus.ToString());
                    _args.Add("fdReportId", fdKeyId.ToString());
                    _args.Add("fdType", "安全隐患");

                    this.riskRecord.LoadView<EFTabItem>("ZMCLR2", _args);//ZMCLR2 安全隐患
                }
                else if (_item.Name == "riskCP")
                {
                    var fdDate = this.Grid.SelectedItem.GetType().GetProperty("fdReportDate").GetValue(this.Grid.SelectedItem, null);
                    var fdKeyId = this.Grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.Grid.SelectedItem, null);
                    var fdStatus = this.Grid.SelectedItem.GetType().GetProperty("fdStatus").GetValue(this.Grid.SelectedItem, null);

                    Dictionary<string, string> _args = new Dictionary<string, string>();
                    _args.Add("fdRiskDate", fdDate.ToString());
                    _args.Add("fdStatus", fdStatus.ToString());
                    _args.Add("fdReportId", fdKeyId.ToString());
                    _args.Add("fdType", "工程质量及文明生产");

                    this.riskCP.LoadView<EFTabItem>("ZMCLR2", _args);//ZMCLR2 工程质量及文明生产
                }
                else if (_item.Name == "riskPG")
                {
                    var fdDate = this.Grid.SelectedItem.GetType().GetProperty("fdReportDate").GetValue(this.Grid.SelectedItem, null);
                    var fdKeyId = this.Grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.Grid.SelectedItem, null);
                    var fdStatus = this.Grid.SelectedItem.GetType().GetProperty("fdStatus").GetValue(this.Grid.SelectedItem, null);

                    Dictionary<string, string> _args = new Dictionary<string, string>();
                    _args.Add("fdDate", fdDate.ToString());
                    _args.Add("fdStatus", fdStatus.ToString());
                    _args.Add("fdReportId", fdKeyId.ToString());

                    this.riskPG.LoadView<EFTabItem>("ZMCLR7", _args);//ZMCLR7 工程质量和文明生产日评估
                }
                else
                {
                    var fdDate = this.Grid.SelectedItem.GetType().GetProperty("fdReportDate").GetValue(this.Grid.SelectedItem, null);
                    var fdKeyId = this.Grid.SelectedItem.GetType().GetProperty("fdKeyId").GetValue(this.Grid.SelectedItem, null);
                    var fdStatus = this.Grid.SelectedItem.GetType().GetProperty("fdStatus").GetValue(this.Grid.SelectedItem, null);

                    Dictionary<string, string> _args = new Dictionary<string, string>();
                    _args.Add("fdDate", fdDate.ToString());
                    _args.Add("fdStatus", fdStatus.ToString());
                    _args.Add("fdReportId", fdKeyId.ToString());

                    this.civilizationProduce.LoadView<EFTabItem>("ZMCLR6", _args);//ZMCLR6 其他问题
                }
            }
            else
            {
                if (null != _item)
                {
                    _item.Content = null;
                }
            }
        }
    }
}
